

function main( ui ) {
   var json = ui.getQueryParameters().getFirst("json");
   if ( json == null ){
        var ex = "{ exception: { message: 'no param provided'} }";
        return ex;
    }
      var o = new JSONObject( json );
     
   
      var  TIMEZONE_START_TIMEOFFSETTENANT_JSON_KEY="TIMEZONE_START_TIMEOFFSETTENANT_JSON_KEY";
      var  TIMEZONE_START_TIMEOFFSETAPPOINTMENT_JSON_KEY="TIMEZONE_START_TIMEOFFSETAPPOINTMENT_JSON_KEY";
      var  TIMEZONE_START_TIMEOFFSETEMPLOYEE_JSON_KEY="TIMEZONE_START_TIMEOFFSETEMPLOYEE_JSON_KEY";
      var  TIMEZONE_END_TIMEOFFSETTENANT_JSON_KEY="TIMEZONE_END_TIMEOFFSETTENANT_JSON_KEY";
      var  TIMEZONE_END_TIMEOFFSETAPPOINTMENT_JSON_KEY="TIMEZONE_END_TIMEOFFSETAPPOINTMENT_JSON_KEY";
      var  TIMEZONE_END_TIMEOFFSETEMPLOYEE_JSON_KEY="TIMEZONE_END_TIMEOFFSETEMPLOYEE_JSON_KEY";
  
      var  TIMEZONE_TENANT_JSON_KEY="TIMEZONE_TENANT_JSON_KEY";
      var  TIMEZONE_EMPLOYEE_JSON_KEY="TIMEZONE_EMPLOYEE_JSON_KEY";
      var  TIMEZONE_APPOINTMENT_JSON_KEY="TIMEZONE_APPOINTMENT_JSON_KEY";
      
      var  DATESTART_JSON_APPOINTMENT_KEY="DATESTART_APPOINTMENT_MIN";
      var  DATEEND_JSON_APPOINTMENT_KEY="DATEEND_APPOINTMENT_MIN";
      
      var timeStartOffsetTenant        = o.getInt(TIMEZONE_START_TIMEOFFSETTENANT_JSON_KEY);
      var timeStartOffsetAppointment   = o.getInt(TIMEZONE_START_TIMEOFFSETAPPOINTMENT_JSON_KEY);
      var timeStartOffsetEmployee      = o.getInt(TIMEZONE_START_TIMEOFFSETEMPLOYEE_JSON_KEY);

      var timeEndOffsetTenant        = o.getInt(TIMEZONE_END_TIMEOFFSETTENANT_JSON_KEY);
      var timeEndOffsetAppointment   = o.getInt(TIMEZONE_END_TIMEOFFSETAPPOINTMENT_JSON_KEY);
      var timeEndOffsetEmployee      = o.getInt(TIMEZONE_END_TIMEOFFSETEMPLOYEE_JSON_KEY);

      var tzTenant                = o.getString(TIMEZONE_TENANT_JSON_KEY);    
      var tzEmployee              = o.getString(TIMEZONE_EMPLOYEE_JSON_KEY);
      var tzAppointment           = o.getString(TIMEZONE_APPOINTMENT_JSON_KEY);
      
      var startDateAppointment    = o.getString(DATESTART_JSON_APPOINTMENT_KEY);
      var endDateAppointment      = o.getString(DATEEND_JSON_APPOINTMENT_KEY); 
  
     var calendar = java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone(tzAppointment));
     var formatter = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
     var dateStart = formatter.parse(startDateAppointment);  
     var dateEnd = formatter.parse(endDateAppointment) ;
     calendar.setTime(dateStart);
     
     var calculate=((-1)*timeStartOffsetTenant *60*1000)-((-1)*timeStartOffsetAppointment*60*1000);
     var timeStartTenant = new java.util.GregorianCalendar(java.util.TimeZone.getTimeZone(tzTenant));
     timeStartTenant.setTimeInMillis(calendar.getTimeInMillis() +calculate); 

     calculate=((-1)*timeStartOffsetEmployee *60*1000)-((-1)*timeStartOffsetAppointment*60*1000);
     var timeStartEmployee = new java.util.GregorianCalendar(java.util.TimeZone.getTimeZone(tzEmployee));
     timeStartEmployee.setTimeInMillis(calendar.getTimeInMillis() + calculate); 

     calendar.setTime(dateEnd);
     calculate=((-1)*timeEndOffsetTenant *60*1000)-((-1)*timeEndOffsetAppointment*60*1000);
     var timeEndTenant = new java.util.GregorianCalendar(java.util.TimeZone.getTimeZone(tzTenant));
     timeEndTenant.setTimeInMillis(calendar.getTimeInMillis() + calculate); 
    
     calculate=((-1)*timeEndOffsetEmployee *60*1000)-((-1)*timeEndOffsetAppointment*60*1000);
     var timeEndEmployee = new java.util.GregorianCalendar(java.util.TimeZone.getTimeZone(tzEmployee));
     timeEndEmployee.setTimeInMillis(calendar.getTimeInMillis() + calculate); 
     
     var formatter2 = new java.text.SimpleDateFormat("yyyy'/'MM'/'dd ' ' HH':'mm':'ss");
     var resultJSON=new JSONObject();
     resultJSON.put("TIME_START_TENANT", formatter2.format( timeStartTenant.getTime()));
     resultJSON.put("TIME_START_EMPLOYEE",formatter2.format(timeStartEmployee.getTime()));
     resultJSON.put("TIME_END_TENANT",formatter2.format(timeEndTenant.getTime()));
     resultJSON.put("TIME_END_EMPLOYEE",formatter2.format(timeEndEmployee.getTime()));

    return resultJSON.toString();
  
}

 